#include "ddas_definitions.h"
#include "ddas_validator_helpers.h"

#include <stdio.h>
#include <string.h>
#include <iostream>

int getFileSize(FILE* fp)
{
        int bytesRead=0;
        while(fgetc(fp)!=EOF)
        {
                bytesRead++;
        }

        rewind(fp);
        return bytesRead;
}

char* filePointerToCharPointer(FILE* fp)
{
        char* fileData=(char*)malloc(getFileSize(fp));
        int index=0;
        int c;

        while((c=fgetc(fp))!=EOF)
        {
                fileData[index++]=c;
        }

        rewind(fp);
        return fileData;
}

char** getDefNumbers(char* xml)
{
        char* space=" ";
        char* remainingXML=(char*)malloc(strlen(xml));
        char* curString=(char*)malloc(strlen(xml));
	char** defNumbers=(char**)malloc(EXPECTED_NUMBER_OF_DATAROWS);

	int index=0;
        while((remainingXML=strstr(xml,"defNumber="))!=NULL)
        {
                strcpy(xml,remainingXML+strlen("defNumber="));
                curString=mystrtok(xml,space);

		defNumbers[index++]=curString;
                //puts(curString);
                //puts(xml);
        }

	return defNumbers;
}

char* mystrtok(char* string, char* delim)
{
        int strLen=strlen(string);
        char* result=(char*)malloc(strlen(string));
        char* space=" ";

        for(int i=0; i<strLen; i++)
        {
                if(string[i]==*space)
                {
                        //puts("MATCH FOUND");
                        return result;
                }

                result[i]=string[i];
        }

        //puts("NO MATCHES");
        return "NO MATCHES";
}

